//
//  QuickRegisterViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "QuickRegisterViewController.h"
#import "LoginViewController.h"
#import "AppSetting.h"
#import "UCenterViewController.h"

#define FAST_TEXT @"快速注册"
#define VALIDATE_TEXT @"手机验证"

@implementation QuickRegisterViewController

- (id)initWithType:(RegisterType)type
{
    self = [super initWithNibClass:[QuickRegisterViewController class]];
    if (self) {
        _type = type;
        _dataSource = [[QuickRegisterDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    NSString *title = _type == FastRegister ? FAST_TEXT : VALIDATE_TEXT;
    
    [self createLeftButton];
    [self createRightButton:@"登录"];
    [self createNavigation:title];
    
    [self.view addTarget:self action:@selector(viewTapped:)];
    [_contentView drawBorder];
}

- (void)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonClick:(id)sender
{
    [self pushOrPopViewController:[LoginViewController class]];
}

- (IBAction)buttonPress:(id)sender
{
    [self viewTapped:nil];
    
    _tel = _phoneNumberTF.text;
    _verify = _verifyNumberTF.text;
    UIButton* button = (UIButton *)sender;
    
    if (button.tag == 0) {
        [_dataSource sendSMS];
    }
    
    if (button.tag == 1) {
        if (![self checkForm]) return;
        [_dataSource fastRegister];
    }
}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    [_phoneNumberTF resignFirstResponder];
    [_verifyNumberTF resignFirstResponder];
}

- (BOOL)checkForm
{
    if (_tel.length <= 0) {
        [AppDelegate showMessage:@"手机号码不能为空!"];
        return NO;
    }
    
    if (_verify.length <= 0) {
        [AppDelegate showMessage:@"验证码不能为空!"];
        return NO;
    }
    
    if (_isRegister && [_tel isEqualToString:_registerTel]) {
        [AppDelegate showMessage:@"该手机号码已注册，请重新填写！"];
        return NO;
    }
    
    return YES;
}

@end

//==========================================================================
#pragma ===================数据源类实现=======================================
//==========================================================================

#define SEND_SMS_TAG 1001
#define REGISTER_TAG 1002
#define VALIDATE_CODE_TAG 1003

@implementation QuickRegisterDataSource

- (id)initWithVC:(id)Controller
{
    self = [super init];
    if(self) {
        self.Controller = Controller;
    }
    return self;
}

- (void)sendSMS
{
    [SVProgressHUD show];
    
    NSString *url = S_EMPTY;
    NSString *tel = _Controller.tel;
    
    if (_Controller.type == FastRegister) {
        url = [NSString stringWithFormat:API_F_MESSAGE, tel];
    }
    
    if (_Controller.type == ValidateTel) {
        url = [NSString stringWithFormat:API_R_MESSAGE, tel, [AppSetting userName]];
    }
    
    [self get:url tag:SEND_SMS_TAG];
}

- (void)fastRegister
{
    [SVProgressHUD show];
    
    NSString *url = S_EMPTY;
    NSString *tel = _Controller.tel;
    NSString *verify = _Controller.verify;
    
    if (_Controller.type == FastRegister) {
        url = [NSString stringWithFormat:API_FAST_REGISTER, verify, tel];
    }
    
    if (_Controller.type == ValidateTel) {
        url = [NSString stringWithFormat:API_REGIST_DONE, verify, tel, [AppSetting userName]];
    }
    
    [self get:url tag:REGISTER_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    [self processRequest:data withTag:request.tag];
}

- (void)processRequest:(NSDictionary *)data withTag:(int)tag
{
    int state = [[data objectForKey:@"state"] intValue];
    if (tag == SEND_SMS_TAG) {
        if (state == 1) {
            _Controller.isRegister = NO;
            [SVProgressHUD dismissWithSuccess:@"验证码已发送请注意查收！"];
        }
        if (state == 2) {
            _Controller.isRegister = YES;
            [SVProgressHUD dismissWithError:@"当前手机号码已被注册过！"];
        }
    }
    
    if (tag == REGISTER_TAG) {
        if (state == 1) {
            [SVProgressHUD dismissWithSuccess:S_EMPTY];
            
            [AppSetting setIsLogin:YES];
            [AppSetting setTel:_Controller.tel];
            
            if (_Controller.type == FastRegister) {
                [AppSetting setUserName:_Controller.tel];
            }
            
            UCenterViewController *memberVC = [[UCenterViewController alloc] init];
            [_Controller pushViewController:memberVC];
        }
        
        if (state == 2) {
            [SVProgressHUD dismissWithError:@"验证码已过期，请重新获取！"];
        }
    }
}

@end

